package com.kolon.framework.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.springframework.web.servlet.view.AbstractView;

/**
 * XML 뷰
 * @author Stone
 */
public class XmlView extends AbstractView {
	
	/** 디폴트 캐릭터셋 */
	private static final String DEFAULT_ENCODING = "utf-8";
	/** 디폴트 컨텐츠 타입 */
	private static final String DEFAULT_CONTENT_TYPE = "application/xml";
	/** 캐릭터 셋 */
	private String encoding;
	/** 컨텐츠 타입 */
	private String contentType;
	
	/**
	 * 생성자로서 디폴트속성을 부여 함
	 */
	public XmlView() {
		setEncoding(DEFAULT_ENCODING);
		setContentType(DEFAULT_CONTENT_TYPE);
	}
	
	/**
	 * 뷰  리졸버에서 뷰를 실행될 때 호출되는 함수
	 */
	@SuppressWarnings("rawtypes")
	public void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 컨트롤러에서 넘어온 데이터
		Document xmlDoc = (Document) model.get("xmlDoc");
		// XML 설정 
		XMLOutputter outputter = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setEncoding("utf-8");
		outputter.setFormat(format);
		String xmlStr = outputter.outputString(xmlDoc);
		// 응답 설정
		response.setContentType(getContentType());
		response.setCharacterEncoding(getEncoding());
		response.setHeader("Cache-Control", "no-cache");
		response.setContentLength(xmlStr.getBytes(getEncoding()).length);
		response.getWriter().print(xmlStr);
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	public String getEncoding() {
		return encoding;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}	

}
